package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.Mbom;
import com.efast.dmom.mes.bean.MbomItem;
import com.efast.dmom.mes.bean.MbomRsBom;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.RouteStep;
import com.efast.dmom.mes.datahandlers.entity.MbomDataHandler;
import com.efast.dmom.mes.datahandlers.entity.MbomItemDataHandler;
import com.efast.dmom.mes.datahandlers.entity.MbomRsBomDataHandler;
import com.efast.dmom.mes.datahandlers.entity.RouteStepDataHandler;
import com.efast.dmom.mes.entity.MbomItemEntity;
import com.efast.dmom.mes.service.MbomItemService;

@Component
public class MbomItemServiceImpl implements MbomItemService{
	@Autowired
	private MbomItemDataHandler mbomItemDataHandler;
	@Autowired
	private MbomDataHandler mbomDataHandler;
	@Autowired
	private RouteStepDataHandler routeStepDataHandler;
	@Autowired
	private MbomRsBomDataHandler mbomRsBomDataHandler;
	
	@Override
	public ResultBean save(MbomItem obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		
		resultBean = checkSaveDataInfo(obj);
		
		if(resultBean.isSuccess()) {
			return mbomItemDataHandler.save(obj);
		}else {
			return resultBean;
		}
	}
	private ResultBean checkSaveDataInfo(MbomItem obj) {
		ResultBean resultBean = new ResultBean();
		
		
		MbomItemEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
//		private long mbomId;
//		private long routeStepId;
//		private long mbomRsBomId;
		long mbomId = entity.getMbomId();
		long routeStepId = entity.getRouteStepId();
		long mbomRsBomId = entity.getMbomRsBomId();
		Mbom mbom = mbomDataHandler.getById(mbomId);
		RouteStep routeStep = routeStepDataHandler.getRouteStepByKey(routeStepId);
		MbomRsBom mbomRsBom = mbomRsBomDataHandler.getById(mbomRsBomId);
		if(mbom == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("MbomId:"+mbomId+" error");
			return resultBean;
		}
		if(routeStep == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("routeStepID:"+routeStepId+" error");
			return resultBean;
		}
		if(mbomRsBom == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("mbomRsBomId:"+mbomRsBomId+" error");
			return resultBean;
		}
		MbomItem getbyReleateId = mbomItemDataHandler.getbyReleateId(mbomId,routeStepId,mbomRsBomId);
		if(getbyReleateId != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("the MbomItem is exist!");
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}
	
	@Override
	public ResultBean delete(MbomItem entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean update(MbomItem entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean save(List<MbomItem> list) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean update(List<MbomItem> list) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean delete(List<MbomItem> list) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MbomItem getById(long id) {
		// TODO Auto-generated method stub
		return null;
	}

}
